﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PROJECT_BOMBER.BLL
{
    public class GroundCollision : CollisionStrategy
    {
        public override void Collision(ElementPhysique element2, ElementMove element)
        {

                #region character

                int[] nbpx;

                if (element2.GetType() == typeof(Level))
                {
                    nbpx = ((Level)element2).CalcIntersect(element);
                }
                else
                {
                    nbpx = element2.CalcIntersect(element);
                }

                double Angle = element2.AnglePX(element);

                bool blockleft = false;
                bool blockright = false;
                bool blocktop = false;
                bool blockbottom = false;

                if (nbpx[0] > 0 && nbpx[3] > 0)//tl
                {
                    blockleft = true;
                }
                if (nbpx[0] > 0 && nbpx[1] > 0) // br
                {
                    blocktop = true;
                }
                if ((nbpx[2] > 0 && nbpx[3] > 0) || (Angle != 0 && Angle < 30))//tr
                {
                    if (element.Speed.Y >= (int)(element.Speedmax.Y/2))
                    {
                        element.Dead = true;
                    }
                    blockbottom = true;
                }
                else
                {
                    blockbottom = false;
                }
                if (nbpx[1] > 0 && nbpx[2] > 0)//bl
                {
                    blockright = true;
                }



                if (blockbottom)
                {
                    if (element.Jump > 0 && element.Speed.Y < 0)
                    {

                    }
                    else
                    {
                        if (Angle < 20 && (Angle > 1 || Angle < -1))
                        {
                            element.Speed = new Microsoft.Xna.Framework.Vector2(element.Speed.X, -(float)(Math.Tan(Angle * (Math.PI / 180)) * (element.Speed.X * (Math.PI / 180))) * 180 / (float)Math.PI);
                        }
                        else
                        {
                            element.Speed = new Microsoft.Xna.Framework.Vector2(element.Speed.X, 0);
                        }
                        element.Onground++;
                        element.ActiveSpeedX = true;
                        element.Jump = 0;

                        //element.Position.Y -= -3 + (nbpx[2] + nbpx[3]) / element.Width;
                        element.Position = new Vector2(element.Position.X, element.Position.Y + 3 - (nbpx[2] + nbpx[3]) / element.Width);
                    }

                }

                if (blocktop && element.Speed.Y < 0)
                {
                    element.Speed = new Microsoft.Xna.Framework.Vector2(element.Speed.X, 0);
                    //  element.Position.Y += (nbpx[1] + nbpx[2]) / element.Width;
                    element.Position = new Vector2(element.Position.X, element.Position.Y + (nbpx[1] + nbpx[2]) / element.Width);
                }
                if (blockleft && element.Speed.X < 0)
                {
                    element.Speed = new Microsoft.Xna.Framework.Vector2(-1, element.Speed.Y);
                    //element.Position.X += -1 +(nbpx[3] + nbpx[0]) / element.Height;
                    element.Position = new Vector2(element.Position.X - 1 + (nbpx[3] + nbpx[0]) / element.Height, element.Position.Y);
                    element.JumpWall = true;
                }
                if (blockright && element.Speed.X > 0)
                {
                    element.Speed = new Microsoft.Xna.Framework.Vector2(1, element.Speed.Y);
                    //element.Position.X -= - 4 + (nbpx[1] + nbpx[2]) / element.Height;
                    element.Position = new Vector2(element.Position.X + 4 - (nbpx[1] + nbpx[2]) / element.Height, element.Position.Y);
                    element.JumpWall = true;
                }

                #endregion

        }

        

        public override void Collision(ElementMove element, ElementMove element2)
        {
            throw new NotImplementedException();
        }
    }
}
